package net.xdclass.controller;


import net.xdclass.controller.request.LinkGroupAddRequest;
import net.xdclass.controller.request.LinkGroupUpdateRequest;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.model.LinkGroupDO;
import net.xdclass.model.vo.LinkGroupVO;
import net.xdclass.service.ILinkGroupService;
import net.xdclass.service.impl.LinkGroupServiceImpl;
import net.xdclass.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hr
 * @since 2025-08-04
 */
@RestController
@RequestMapping("/api/group/v1")
public class LinkGroupController {
    @Autowired
    private ILinkGroupService linkGroupService;
    /**
     * 添加分组
     * @param request
     * @return
     */
    @PostMapping("/add")
    public JsonData add(@RequestBody LinkGroupAddRequest request) {
        int rows = linkGroupService.add(request);
        return rows ==1?JsonData.buildSuccess():JsonData.buildResult(BizCodeEnum.GROUP_ADD_FAIL);
    }
    /**
     * 删除分组
     * @param groupId
     * @return
     */
    @DeleteMapping("/delete/{group_id}")
    public JsonData delete(@PathVariable("group_id") Long groupId){
        int rows = linkGroupService.del(groupId);
        return rows ==1?JsonData.buildSuccess():JsonData.buildResult(BizCodeEnum.GROUP_NOT_EXIST);
    }
    /**
     * 分组详情
     * @param groupId
     * @return
     */
    @GetMapping("/detail/{group_id}")
    public JsonData detail(@PathVariable("group_id") Long groupId){
        LinkGroupVO linkGroupVO = linkGroupService.detail(groupId);
        return JsonData.buildSuccess(linkGroupVO);
    }

    /**
     * 查询用户所有分组
     * @return
     */
    @GetMapping("/list")
    public JsonData findUserAllLinkGroup(){
        List<LinkGroupVO> linkGroupVOList = linkGroupService.listAllGroup();
        return JsonData.buildSuccess(linkGroupVOList);
    }

    /**
     * 更新分组
     * @param request
     * @return
     */
    @PutMapping("/update")
    public JsonData update(@RequestBody LinkGroupUpdateRequest request){
        int rows = linkGroupService.updateById(request);
        return rows ==1?JsonData.buildSuccess():JsonData.buildResult(BizCodeEnum.GROUP_OPER_FAIL);
    }

}
